// 初始化调试工具

const vConsole = new window.VConsole();
setTimeout(()=>{
    vConsole.setSwitchPosition(0,500)
},300)

// 设置地图的安全密钥
window._AMapSecurityConfig  ={
    securityJsCode: "c60c7bd4e3f2028e1a79514eee751d82"
}
var hwMap;
var AMapIns;
var nowMark;
var nowLayer;
var siteServer;
var h5port;
var baseHost;
var polyLine;
var polyLinePath=[]
var endLocaInfo;
var driving;

var getLines = async ({origin,destination})=>{
    const res = await axios.post(`http:${baseHost}/driverPlan`,{
        origin,
        destination
    })
    const alltmcs = []
    const steps = res.data.route.paths[0].steps;
    for(const item of steps){
        for(const it of item.tmcs){
            alltmcs.push({
                ...it,
                po
            })
        }
    }
}

const handleMessage = (event)=>{
    console.log('从ets发送过来的数据',event.data);
    const { type,payload} = JSON.parse(event.data);
    if(type === 'setCenter'){
        const gcjloc = coordtransform.wgs84togcj02(payload.lng,payload.lat)
        const centerPoint = new AMapIns.LngLat(gcjloc[0],gcjloc[1])
        hwMap.setCenter(centerPoint);
        hwMap.setZoom(16);
        nowLayer.setPosition(centerPoint)
        // gps的坐标信息 不能直接是用在 地图框架当中 需要进行一定的转换
        // AMapIns.convertFrom([payload.lng, payload.lat], 'gps', (_,result)=>{
        //     const centerPoint = result.locations[0]; // gps坐标转换之后的坐标点
        //     hwMap.setCenter(centerPoint);
        //     hwMap.setZoom(16);
        //     nowLayer.setPosition(centerPoint)
        // })
    }

    if(type === 'zoom-'){
        hwMap.zoomOut();
        hwMap.resize();
    }
    if(type === 'zoom+'){
        hwMap.zoomIn();
        hwMap.resize();
    }
    // 轨迹划线

    if(type==='drawLine'){
        const startLoc = payload.startLocationInfo.location.split(',');
        const endLoc = payload.endLocationInfo.location.split(',');
        // const gcjloc = coordtransform.wgs84togcj02(endLoc[0],endLoc[1])
        endLocaInfo = endLoc;
        const startCp = coordtransform.wgs84togcj02(startLoc[0],startLoc[1]);


        AMapLoader.load({
            key: "c4a9fc30889509165c7ae94811bdb5c9",
            version: "2.0"
        }).then( async AMap=>{
            AMapIns = AMap;

            const centerPoint = new AMapIns.LngLat(startCp[0],startCp[1])
            hwMap = new AMap.Map("map",{
                viewMode: '2d' ,// 默认使用2d地图
                zoom: 16, // 地图的默认缩放等级
                center: endLoc
            })


            const mkcp = new AMapIns.LngLat(startCp[0], startCp[1])
            nowMark = new AMap.Marker({
                position:mkcp,
                icon: new AMap.Icon({
                    image:`http://static.1775.net.cn/fmcar.png`,
                    imageSize: new AMap.Size(36,36)
                }),
                anchor: 'top-left', //  设置这个可以避免地图缩放的时候导致的图标位移变化
                offset: new AMap.Pixel(-18,-18)
            })
            hwMap.add(nowMark)



            // 司机实际行驶的轨迹
            polyLine = new AMapIns.Polyline({
                zIndex: 9,
                strokeColor: 'gray',
                borderWeight:2, // 线条宽度，默认为 1
                showDir: true,
                strokeWeight:8,
            })
            hwMap.add(polyLine)

            AMapIns.plugin('AMap.Driving',()=>{
                 driving = new AMapIns.Driving({
                    map: hwMap
                })
                driving.search(centerPoint,endLoc,function(_,result){})
            })


        })


    }

    //  司机的位置变化
    if(type === 'updateDriveLocation'){

        const cp =  coordtransform.wgs84togcj02(payload.lng,payload.lat );
        const mkcp = new AMapIns.LngLat(cp[0], cp[1])
        nowMark.setPosition(mkcp)
        polyLinePath.push(cp);
        polyLine.setPath(polyLinePath)
    }

    // 司机到达
    if(type === 'driverArrive'){
            const infoWindow = new AMapIns.InfoWindow({
                content:`<div>司机已经到达</div>`,
                position: new AMapIns.LngLat(endLocaInfo[0],endLocaInfo[1]),
                anchor:'bottom-center',
                size:  new AMapIns.Size(100,30),
                offset: new AMap.Pixel(0,-25)
            })
            infoWindow.open(hwMap)
    }
    // 司机开始去终点 重新规划路径
    if(type === 'reDraw'){
        const temstart = payload.startLoc.split(',')
        const gcjloc = coordtransform.wgs84togcj02(temstart[0],temstart[1]);

        const temend = payload.endLoc.split(',')
        console.log()
        driving.search(gcjloc,temend,()=>{})
        polyLinePath = []
        polyLine.setPath(polyLinePath)
    }

}


//  响应postMessage事件
window.addEventListener('message', function(event){
    console.log('与应用侧建立了通讯');
    if(event.data === '__init_port__' ){
        h5port = event.ports[0];
        baseHost = event.ports[1];
        h5port.onmessage = handleMessage
        h5port.postMessage(JSON.stringify({type:'webready'}))

    }
})